import {createRouter, createWebHashHistory} from 'vue-router'

import login from '../pages/login/login.vue'
import regist from '../pages/register/regist.vue'
import home from '../pages/home/home.vue'
import {defineUser} from "../stores/userStore.js";
import pinia from "../stores/pinia.js";
import {getToken} from "../utils/token-tuils.js";
import article from "../pages/article/article.vue";
import changeArticle from "../pages/changeArticle/changeArticle.vue";

let router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            component: login
        },
        {
            path: "/login",
            component: login
        },
        {
            path: "/regist",
            component: regist
        },
        {
            path: "/home",
            component: home
        },
        {
            path:"/article",
            component: article
        },
        {
            path:"/changeArticle",
            component: changeArticle
        }


    ]
})
const user = defineUser(pinia)
//全局前置守卫 - 阻止未登录用户访问
router.beforeEach(async (to, from, next) => {
    const token = getToken()
    if(to.path === '/login' || to.path === '/regist' ){
        next()
    }else{
        if (token){
            next()
        }else {
        next('/login')
        }
    }

});

export default router